package com.walk.zworld.mvvm.vm

import androidx.lifecycle.LiveData
import com.walk.zworld.mvvm.repository.TodoRepository
import com.base.lib.base.event.SingleLiveEvent
import com.base.lib.base.mvvm.viewmodel.BaseRefreshViewModel
import com.base.lib.httpUtils.TodoBean

class TodoActivityViewModel : BaseRefreshViewModel<TodoBean>() {

    private val repository = TodoRepository()
    private var updateTodoById = SingleLiveEvent<Any>()
    private var deleteTodoById = SingleLiveEvent<Any>()
    private var addTodo = SingleLiveEvent<Any>()
    private var updateTodo = SingleLiveEvent<Any>()

    fun updateTodoById(id: Int, status: Int): LiveData<Any> {
        launchUI {
            val res = repository.updateTodoById(id, status)
            updateTodoById.value = res.data
        }
        return updateTodoById
    }

    fun deleteTodoById(page: Int): LiveData<Any> {
        launchUI {
            val res = repository.deleteTodoById(page)
            deleteTodoById.value = res.data
        }
        return deleteTodoById
    }

    fun addTodo(map: MutableMap<String, Any>): LiveData<Any> {
        launchUI {
            val res = repository.addTodo(map)
            addTodo.value = res.data
        }
        return addTodo
    }

    fun updateTodo(id: Int, map: MutableMap<String, Any>): LiveData<Any> {
        launchUI {
            val res = repository.updateTodo(id, map)
            updateTodo.value = res.data
        }
        return updateTodo
    }

    fun getTodoList(page: Int, map: MutableMap<String, Any>): LiveData<List<TodoBean>> {
        launchUI {
            val res = repository.getTodoList(page, map)
            mData.value = res.data.datas
        }
        return mData
    }

    override fun refreshData(params: Any?) {
        page = 0
        if (params is MutableMap<*, *>) getTodoList(page, params as MutableMap<String, Any>);
    }

    override fun loadMore(params: Any?) {
        page ++
        if (params is MutableMap<*, *>) getTodoList(page, params as MutableMap<String, Any>);
    }
}